//
//  TTChatLocalCell.swift
//  TTProduct
//
//  Created by Toj on 3/30/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Cocoa

class TTChatLocalCell: TTChatCell {
    
    override var chatMessage: TTChatMessage? {
        didSet {
            super.chatMessage = chatMessage
            guard let chat = chatMessage else { return }
            
            emojiImageView.isHidden = chat.type != .emoji
            massageLabel.isHidden = chat.type != .message
            massageImageView.isHidden = chat.type != .message
            
            nicknameLabel.stringValue = chat.nickname
           
            // message
            if chat.type == .message {
                guard let message = chat.message else { return }
                massageLabel.stringValue = message.text
                remakeLayoutSubviews()
                return
            }
            
            // emoji
            if chat.type == .emoji {
                guard let emoticon = chat.emoticon else { return }
                let imagePath = emoticon.largeImagePath ?? ""
                emojiImageView.image = NSImage(contentsOfFile: imagePath)
            }
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        massageImageView.image = NSImage(named:"liveChatBGLocal")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setupMakeLayoutSubviews() {
        super.setupMakeLayoutSubviews()
        
        makeLayoutSubviews()
    }
}

extension TTChatLocalCell {
    
    private func remakeLayoutSubviews() {
        guard let message = chatMessage?.message else { return }
        
        massageLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(nicknameLabel.snp.bottom).offset(15)
            make.right.equalTo(-25)
            make.width.equalTo(message.textWidth)
        }
        
        massageImageView.snp.remakeConstraints { (make) in
            make.right.equalTo(massageLabel.snp.right).offset(10)
            make.bottom.equalTo(massageLabel.snp.bottom).offset(10)
            make.top.equalTo(massageLabel.snp.top).offset(-10)
            make.left.equalTo(massageLabel.snp.left).offset(-6)
        }
    }
    
    private func makeLayoutSubviews() {
        
        emojiImageView.snp.remakeConstraints { (make) in
            make.top.equalTo(nicknameLabel.snp.bottom).offset(5)
            make.right.equalTo(nicknameLabel)
            make.width.equalTo(36)
            make.height.equalTo(36)
        }
    }
}
